package com.school.controller;

import com.school.dto.user.*;
import com.school.model.PageModel;
import com.school.model.ResultModel;
import com.school.model.User;
import com.school.service.UserService;
import com.school.tool.Result;
import com.school.vo.UserInfoVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller()
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public @ResponseBody ResultModel login(@Validated LoginDTO dto, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getFieldErrors().get(0).getDefaultMessage());
        }
        User user=userService.login(dto);
        if(user!=null){
            return Result.success(user);
        }else{
            return Result.error("用户名或密码错误！");
        }
    }


    @PostMapping("/student")
    public @ResponseBody ResultModel insertStudentUser(@Validated InsertStudentUserDTO dto, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getFieldErrors().get(0).getDefaultMessage());
        }
        User user=new User();
        Date date=new Date(dto.getInSchool());
        BeanUtils.copyProperties(dto,user);
        user.setInSchoolTime(date);
        int resultCode=userService.insertSelective(user);
        if(resultCode>0){
            return Result.success();
        }else if(resultCode==-1){
            return Result.error("添加失败，用户名重复！");
        }else if(resultCode==-2){
            return Result.error("添加失败，必须选择任课教师！");
        }else{
            return Result.error("添加失败！");
        }
    }

    @PostMapping("/teacher")
    public @ResponseBody ResultModel insertTeacherUser(@Validated InsertTeacherUserDTO dto, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getFieldErrors().get(0).getDefaultMessage());
        }
        User user=new User();
        BeanUtils.copyProperties(dto,user);
        user.setInSchoolTime(new Date(dto.getInSchool()));
        user.setStartWorkTime(new Date(dto.getStartWork()));
        int resultCode=userService.insertSelective(user);
        if(resultCode>0){
            return Result.success();
        }else if(resultCode==-1){
            return Result.error("添加失败，用户名重复！");
        }else{
            return Result.error("添加失败！");
        }
    }

    @GetMapping("/id")
    public @ResponseBody ResultModel userInfoById(@Param("id") Integer id){
        if(id==null){
            return Result.error("id不能为空！");
        }
        User user=userService.selectByPrimaryKey(id);
        user.setUserName(null);
        user.setPassword(null);
        UserInfoVO vo=null;
        if(user!=null){
            vo=new UserInfoVO();
            BeanUtils.copyProperties(user,vo);
        }
        return Result.success(vo);
    }

    @GetMapping()
    public @ResponseBody ResultModel getArrayByParam(UserArrayDTO dto){
        PageModel result =userService.selectArrayByParam(dto);
        return Result.success(result);
    }


    @PostMapping("/id")
    public @ResponseBody ResultModel updateTeacherUser(@Validated UpdateUserDTO dto, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getFieldErrors().get(0).getDefaultMessage());
        }
        User user=new User();
        BeanUtils.copyProperties(dto,user);
        int resultCode=userService.updateByPrimaryKeySelective(user);
        if(resultCode>0){
            return Result.success();
        }else if(resultCode==-2){
            return Result.error("修改失败，必须选择任课教师！");
        }else{
            return Result.error("修改失败！");
        }
    }

    @PostMapping("/{id}")
    public @ResponseBody ResultModel delete(@PathVariable("id") Integer id){
        if(id==null){
            return Result.error("缺少编号!");
        }
        int resultCode=userService.deleteByPrimaryKey(id);
        if(resultCode>0){
            return Result.success();
        }else{
            return Result.error("删除失败！");
        }
    }

}
